package planit.view;
import java.awt.event.ActionEvent;
import java.util.ArrayList;

import javax.swing.AbstractAction;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import planit.controller.ControllerFactory;
import planit.controller.ControllerFactory.ControllerType;
import planit.model.Event;
import planit.model.Model;
import planit.model.Person;
import planit.view.wrappers.EventWrapper;
import planit.view.wrappers.ModelCellRenderer;
import planit.view.wrappers.PersonWrapper;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
@SuppressWarnings("serial")
public class AddPersonToEventDialog extends javax.swing.JDialog {

	{
		// Set Look & Feel
		try {
			javax.swing.UIManager
			.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private JLabel selectEventLabel;
	private JScrollPane jScrollPane1;
	private JScrollPane jScrollPane2;
	private JButton RemovePersonFromEventButton;
	private AbstractAction cancelAction;
	private JScrollPane jScrollPane3;
	private JButton cancelButton;
	private JButton addPerson;
	private JList eventPersonsList;
	private JTextField placeTextField;
	private JLabel descriptionLabel;
	private JLabel eventPersonsLabel;
	private JTextArea descriptionTextArea;
	private JTextField typeTextField;
	private JLabel typeLabel;
	private JLabel placeLabel;
	private JTextField nameTextField;
	private JLabel nameLabel;
	private JList personList;
	private JLabel selectPersonLabel;
	private JList eventList;
	private Model model;

	public AddPersonToEventDialog(Model model) {
		super();
		this.model = model;
		initGUI();
		updateEventList();
		updatePersonList();
		eventList.setSelectedIndex(0);
	}

	private void initGUI() {
		try {
			{
				getContentPane().setLayout(null);
				{
					selectEventLabel = new JLabel();
					getContentPane().add(selectEventLabel);
					selectEventLabel.setText("Select Event");
					selectEventLabel.setBounds(12, 12, 119, 16);
				}
				{
					jScrollPane3 = new JScrollPane();
					getContentPane().add(jScrollPane3);
					jScrollPane3.setBounds(12, 34, 102, 209);
					{
						ListModel eventListModel = new DefaultComboBoxModel(
								new String[] { "Item One", "Item Two" });
						eventList = new JList();
						jScrollPane3.setViewportView(eventList);
						eventList.setModel(eventListModel);
						eventList.setBounds(12, 34, 102, 209);
						eventList
						.addListSelectionListener(new ListSelectionListener() {
							public void valueChanged(
									ListSelectionEvent evt) {
								eventListValueChanged(evt);
							}
						});
					}
				}
				{
					selectPersonLabel = new JLabel();
					getContentPane().add(selectPersonLabel);
					selectPersonLabel.setText("Select Person");
					selectPersonLabel.setBounds(12, 249, 93, 16);
				}
				{
					jScrollPane2 = new JScrollPane();
					getContentPane().add(jScrollPane2);
					jScrollPane2.setBounds(12, 265, 102, 185);
					{
						ListModel personListModel = new DefaultComboBoxModel(
								new String[] { "Item One", "Item Two" });
						personList = new JList();
						jScrollPane2.setViewportView(personList);
						personList.setModel(personListModel);
						personList.setBounds(12, 265, 102, 185);
					}
				}
				{
					nameLabel = new JLabel();
					getContentPane().add(nameLabel);
					nameLabel.setText("Event name:");
					nameLabel.setBounds(126, 34, 79, 16);
				}
				{
					nameTextField = new JTextField();
					getContentPane().add(nameTextField);
					nameTextField.setBounds(126, 56, 246, 23);
				}
				{
					placeLabel = new JLabel();
					getContentPane().add(placeLabel);
					placeLabel.setText("Place:");
					placeLabel.setBounds(126, 79, 49, 16);
				}
				{
					placeTextField = new JTextField();
					getContentPane().add(placeTextField);
					placeTextField.setBounds(126, 95, 246, 23);
				}
				{
					typeLabel = new JLabel();
					getContentPane().add(typeLabel);
					typeLabel.setText("Type:");
					typeLabel.setBounds(126, 124, 39, 16);
				}
				{
					typeTextField = new JTextField();
					getContentPane().add(typeTextField);
					typeTextField.setBounds(126, 140, 246, 23);
				}
				{
					descriptionLabel = new JLabel();
					getContentPane().add(descriptionLabel);
					descriptionLabel.setText("Description:");
					descriptionLabel.setBounds(126, 169, 73, 16);
				}
				{
					descriptionTextArea = new JTextArea();
					getContentPane().add(descriptionTextArea);
					descriptionTextArea.setBounds(126, 185, 246, 58);
				}
				{
					eventPersonsLabel = new JLabel();
					getContentPane().add(eventPersonsLabel);
					eventPersonsLabel.setText("Persons in event:");
					eventPersonsLabel.setBounds(126, 249, 107, 16);
				}
				{
					jScrollPane1 = new JScrollPane();
					getContentPane().add(jScrollPane1);
					jScrollPane1.setBounds(126, 265, 246, 185);
					{
						ListModel eventPersonsListModel = new DefaultComboBoxModel(
								new String[] { "Item One", "Item Two" });
						eventPersonsList = new JList();
						jScrollPane1.setViewportView(eventPersonsList);
						eventPersonsList.setModel(eventPersonsListModel);
						eventPersonsList.setBounds(126, 265, 246, 185);
					}
				}
				{
					addPerson = new JButton();
					getContentPane().add(addPerson);
					addPerson.setAction(ControllerFactory.getController(
							ControllerType.ADD_PERSON_TO_EVENT));
					addPerson.getAction().putValue("AddPersonToEventDialog", this);
					addPerson.setText("Add to Event");
					addPerson.setBounds(12, 456, 102, 28);
				}
				{
					cancelButton = new JButton();
					getContentPane().add(cancelButton);
					getContentPane().add(getRemovePersonFromEventButton());
					cancelButton.setAction(getCancelAction());
					cancelButton.setText("Cancel");
					cancelButton.setBounds(311, 456, 61, 28);
				}
			}
			this.setSize(394, 528);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void eventListValueChanged(ListSelectionEvent evt) {
		if (eventList.getSelectedValue() == null) {
			nameTextField.setText("");
			placeTextField.setText("");
			typeTextField.setText("");
			descriptionTextArea.setText("");
			eventPersonsList.removeAll();
		} else {
			Event temp;
			temp = model.getSchedule().getEvent(((EventWrapper) eventList
					.getSelectedValue()).getId());

			nameTextField.setText(temp.getName());
			placeTextField.setText(temp.getPlace());
			typeTextField.setText(temp.getEventType().getName());
			descriptionTextArea.setText(temp.getDescription());
			updateEventPersonsList();

		}
	}

	public void updateEventList() {
		Event[] event = new Event[0];
		ArrayList<JLabel> allEvents = new ArrayList<JLabel>();
		event = model.getSchedule().getEvents().toArray(event);
		for (int i = 1; i < event.length; i++) {
			allEvents.add(new EventWrapper(event[i]));
		}
		ListModel list2Model = new DefaultComboBoxModel(allEvents.toArray());
		eventList.setModel(list2Model);
		eventList.setCellRenderer(new ModelCellRenderer());
	}

	public void updateEventPersonsList() {
		Event temp;
		temp = model.getSchedule().getEvent(((EventWrapper) eventList.getSelectedValue())
				.getId());

		Person[] person = new Person[0];
		ArrayList<JLabel> allPersons = new ArrayList<JLabel>();
		person = temp.getPersons().toArray(person);
		for (int i = 0; i < person.length; i++) {
			allPersons.add(new PersonWrapper(person[i]));
		}
		ListModel list2Model = new DefaultComboBoxModel(allPersons.toArray());
		eventPersonsList.setModel(list2Model);
		eventPersonsList.setCellRenderer(new ModelCellRenderer());
	}

	public void updatePersonList() {
		Person[] person = new Person[0];
		ArrayList<JLabel> allPersons = new ArrayList<JLabel>();
		person = model.getSchedule().getPersons().toArray(person);
		for (int i = 0; i < person.length; i++) {
			allPersons.add(new PersonWrapper(person[i]));
		}
		ListModel list2Model = new DefaultComboBoxModel(allPersons.toArray());
		personList.setModel(list2Model);
		personList.setCellRenderer(new ModelCellRenderer());
	}

	public int getPersonId() {
		return ((PersonWrapper) personList
				.getSelectedValue()).getId();
	}

	public int getEventId() {
		return ((EventWrapper) eventList
				.getSelectedValue()).getId();
	}
	
	private AbstractAction getCancelAction() {
		if(cancelAction == null) {
			cancelAction = new AbstractAction("Cancel", null) {
				public void actionPerformed(ActionEvent evt) {
					dispose();
				}
			};
		}
		return cancelAction;
	}
	
	public int getPersonInEventId(){
		return ((PersonWrapper)eventPersonsList
				.getSelectedValue()).getId();
	}
	
	private JButton getRemovePersonFromEventButton() {
		if(RemovePersonFromEventButton == null) {
			RemovePersonFromEventButton = new JButton();
			RemovePersonFromEventButton.setAction(ControllerFactory
					.getController(ControllerType.REMOVE_PERSON_FROM_EVENT));
			RemovePersonFromEventButton.getAction().putValue("RemovePersonFromEventDialog", this);
			RemovePersonFromEventButton.setText("Remove from event");
			RemovePersonFromEventButton.setBounds(126, 456, 134, 28);
		}
		return RemovePersonFromEventButton;
	}
}
